﻿using System;
using System.Threading.Tasks;

namespace DaveSexton.Labs.Tests.Category1.Console
{
	public sealed class FileDialogLab : BaseConsoleLab
	{
		protected override void Main()
		{
			RunExperiments();
		}

#if NETFX_CORE
		public async Task OpenFileExperiment()
#else
		public void OpenFileExperiment()
#endif
		{
			try
			{
#if NETFX_CORE
				var file = await UserInputFileToOpenAsync(title: "Open Test");
#else
				var file = UserInputFileToOpen(title: "Open Test");
#endif

				if (file == null)
				{
					TraceLine("Cancelled.");
#if SILVERLIGHT
					TraceWarning("Note: Run this lab Out-Of-Browser or run the UI lab instead.");
#endif
				}
				else
				{
					try
					{
						TraceLine("Selected file: {0}", file.FullName);
					}
					catch (System.Security.SecurityException)
					{
						// SecurityException is thrown by Silverlight apps running in a web browser.
						TraceLine("Selected file: {0}", file.Name);
					}
				}
			}
			catch (NotSupportedException ex)
			{
				TraceError(ex.Message);
			}
		}

#if NETFX_CORE
		public async Task OpenMultipleFilesExperiment()
#else
		public void OpenMultipleFilesExperiment()
#endif
		{
			try
			{
#if NETFX_CORE
				var files = await UserInputFilesToOpenAsync(title: "Open Multiple Test");
#else
				var files = UserInputFilesToOpen(title: "Open Multiple Test");
#endif

				if (files.Count == 0)
				{
					TraceLine("Cancelled.");
#if SILVERLIGHT
					TraceWarning("Note: Run this lab Out-Of-Browser or run the UI lab instead.");
#endif
				}
				else
				{
					foreach (var file in files)
					{
						try
						{
							TraceLine("Selected file: {0}", file.FullName);
						}
						catch (System.Security.SecurityException)
						{
							// SecurityException is thrown by Silverlight apps running in a web browser.
							TraceLine("Selected file: {0}", file.Name);
						}
					}
				}
			}
			catch (NotSupportedException ex)
			{
				TraceError(ex.Message);
			}
		}

#if NETFX_CORE
		public async Task OpenFolderExperiment()
#else
		public void OpenFolderExperiment()
#endif
		{
			try
			{
#if NETFX_CORE
				var folder = await UserInputFolderAsync(title: "Open Folder Test");
#else
				var folder = UserInputFolder(title: "Open Folder Test");
#endif

				if (folder == null)
				{
					TraceLine("Cancelled.");
				}
				else
				{
					TraceLine("Selected folder: {0}", folder);
				}
			}
			catch (NotSupportedException ex)
			{
				TraceError(ex.Message);
			}
		}

#if NETFX_CORE
		public async Task SaveFileExperiment()
#else
		public void SaveFileExperiment()
#endif
		{
			System.IO.FileStream stream = null;
			try
			{
#if NETFX_CORE
				stream = await UserInputFileToSaveAsync(title: "Save Test");
#else
				stream = UserInputFileToSave(title: "Save Test");
#endif

				if (stream == null)
				{
					TraceLine("Cancelled.");
#if SILVERLIGHT
					TraceWarning("Note: Run this lab Out-Of-Browser or run the UI lab instead.");
#endif
				}
				else
				{
					try
					{
						TraceLine("Selected file: {0}", stream.Name);
					}
					catch (System.Security.SecurityException)
					{
						// SecurityException is thrown by Silverlight apps running in a web browser.
						TraceLine("Selected file: [Name Unknown]");
					}
				}
			}
			catch (NotSupportedException ex)
			{
				TraceError(ex.Message);
			}
			finally
			{
				if (stream != null)
				{
#if SILVERLIGHT
					/* During testing an exception was thrown unless the file was closed on the UI thread;
					 * however, other members such as Position, Length, ReadByte and WriteByte didn't throw.
					 */
					Dispatcher.BeginInvoke(stream.Dispose);
#else
					stream.Dispose();
#endif
				}
			}
		}
	}
}